<!-- 项目审批 -->
<template>
	<view >
		<cu-custom bgColor="bg-blue2" :isBack="true" @back="backedPage"><block slot="backText">返回</block><block slot="content">盘点码单编辑</block></cu-custom>
		
		<view>
		
		<form @submit="onSubmit">
			<view class="cu-form-group">
				<view class="title">堆号</view>
				<input v-model="item.heapNum" name="input"></input>
			</view>
			<view class="cu-form-group">
				<view class="title">层号</view>
				<input v-model="item.layerNum" name="input"></input>
			</view>
			<view class="cu-form-group">
				<view class="title">码单号</view>
				<input v-model="item.stakeNum" name="input"></input>
			</view>
			<view class="cu-form-group">
				<view class="title">规格</view>
				<input v-model="item.spec" name="input"></input>
			</view>
			<view class="cu-form-group">
				<view class="title xrequired ">数量</view>
				<input :class="checkRequird && !item.qty ? 'xrwaning' : ''" v-model="item.qty" name="input"></input>
			</view>
			<view class="cu-form-group">
				<view class="title xrequired">重量</view>
				<input :class="checkRequird && !item.qty ? 'xrwaning' : ''" v-model="item.weight" name="input"></input>
			</view>
			<view class="cu-form-group">
				<view class="title">卷号</view>
				<input v-model="item.innerBatch" name="input"></input>
			</view>
			<view class="cu-form-group">
				<view class="title">备注</view>
				<input v-model="item.remark" name="input"></input>
			</view>
			<view>
				<button form-type="submit" class="cu-btn block bg-blue margin-tb-sm lg" :disabled="loading" type="">
					<text :class="loading ? 'cuIcon-loading2 cuIconfont-spin' : ''"></text>保存</button>
			</view>
		</form>
		
		</view>
	</view>
</template>

<script>
	import uniTag from '@/components/uni-tag/uni-tag.vue'
	import uniIcons from '@/components/uni-icons/uni-icons.vue'
	import uniCombox from '@/components/uni-combox/uni-combox.vue'

	import {
		mapGetters
	} from 'vuex'
	import {
		getRandomArrayElements,
		filePreview,
		formatStatus
	} from '@/utils/index.js'
	import { formatDate } from '@/utils/datetime.js'
	export default {
		components: {
			uniTag,
			uniIcons,
			uniCombox
		},
		computed: mapGetters(['user']),
		data() {
			return {
				item: {
					heapNum: '',
					layerNum: '',
					stakeNum: '',
					spec: '',
					innerBatch: '',
					remark: ''
				},
				qtyChkId:0,
				detailId: 0,
				stackId:0,
				loading: false,
				checkRequird: false
			}
		},
		onLoad(options) {
			this.qtyChkId = options.qtyChkId
			this.detailId = options.detailId
			this.stackId = options.stackId
			const qty=options.qty
			const weight=options.weight
			
			if (this.detailId >0 && this.stackId > 0) {
				this.$minApi.getQtyChkStack(this.stackId).then(res => {
					const data = res
					if (data) {
						this.item = data
					}
				}).catch(err => {
					console.log(err)
				}).finally(() => {
				})
			} else {
				this.item.qty=qty
				this.item.weight=weight
				uni.setNavigationBarTitle({
				title: '新增盘点码单'
				})
			}
			
		},
		methods: {
			filePreview,
			formatStatus,
			formatDate,
			onSubmit(e) {  

				this.checkRequird = true

				const _this = this
				
				this.item.qty = Number(this.item.qty)
				this.item.weight = Number(this.item.weight)
				this.item.isResidue = '否'

				if(!Number(this.item.qty)){
					this.item.qty = ''
				}
				if(!Number(this.item.weight)){
					this.item.weight = ''
				}
				
				if(this.item.qty && this.item.weight ){
					
				}else{
					return
				}
				
			    this.loading = true
				
				if (this.stackId > 0) {
					
					_this.$minApi.updateQtyChkStack(this.item.id, this.item).then(res => {
						uni.showToast({
						    title: '修改成功',
						    duration: 2000
						})
						
						setTimeout(() => {
							uni.redirectTo({
								url: `/pages/todo/qtyChk/stack?qtyChkId=${this.qtyChkId}&detailId=${this.detailId}&stackId=0`
							})
						}, 2000)

					}).catch(err => {
						uni.showToast({
							icon: 'none',
						    title: '修改失败',
						    duration: 2000
						})
						console.log(err)
					}).finally(() => {
						this.loading = false
					})
				} else {
					
					this.item.qty = Number(this.item.qty)
					this.item.weight = Number(this.item.weight)
					this.item.isResidue = '否'
					this.item.qtyChkDetailId = Number(this.detailId)
					// this.item.goodsInDetailID = Number(this.item.goodsInDetailID)
					_this.$minApi.createQtyChkStack(this.item).then(res => {
						uni.showToast({
						    title: '添加成功',
						    duration: 2000
						})
						
						setTimeout(() => {
							uni.redirectTo({
								url: `/pages/todo/qtyChk/stack?qtyChkId=${this.qtyChkId}&detailId=${this.detailId}&stackId=0`
							})
						}, 2000)
					}).catch(err => {
						uni.showToast({
							icon: 'none',
						    title: '添加失败',
						    duration: 2000
						})
						console.log(err)
					}).finally(() => {
						this.loading = false
					})
				}
				
			},
			backedPage() {
				uni.redirectTo({
					url: `/pages/todo/qtyChk/stack?qtyChkId=${this.qtyChkId}&detailId=${this.detailId}&stackId=0`
				})
			}
		}
	}
</script>

<style>
.xrequired:after {
	content: '*';
	color: red;
}
.xrwaning{
	border: 1px solid #FF0000;
}

</style>
